Skip to content

ProgramCost metric

Bases: ProgramOperationalMetric

USD over every bound LM/EM in all phases; use for total spend, not per call.

Total provider cost (USD, as reported by litellm) across every LM and EM reached from the program for this run, in every phase: the program's own calls, its judge's (reward) and its optimizer's. The program object itself doesn't pay providers; cost is summed from its bound models.

Example:

program.compile(
    metrics=[
        synalinks.metrics.ProgramCost(),
    ],
)
Source code in synalinks/src/metrics/program_metrics.py
@synalinks_export("synalinks.metrics.ProgramCost")
class ProgramCost(ProgramOperationalMetric):
    """USD over every bound LM/EM in all phases; use for total spend, not per call.

    Total provider cost (USD, as reported by litellm) across every LM
    and EM reached from the program for this run, in every phase: the
    program's own calls, its judge's (reward) and its optimizer's. The program
    object itself doesn't pay providers; cost is summed from its bound models.

    Example:

    ```python
    program.compile(
        metrics=[
            synalinks.metrics.ProgramCost(),
        ],
    )
    ```
    """

    def __init__(self, name="program_cost", description=None):
        super().__init__(name=name, description=description)

    def _read_model_cost(self):
        models = self._language_models + self._embedding_models
        return sum(getattr(model, "cumulated_cost", 0) for model in models)

    def result(self):
        return float(self._delta_model_cost())